package com.yonyou.ucf.mdf.app.isvdxq.firstpart.pandian.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yonyou.ucf.mdf.app.isvdxq.ReturnResult.DataResult;
import com.yonyou.ucf.mdf.app.isvdxq.common.ApiYon;
import com.yonyou.ucf.mdf.app.isvdxq.common.DxqConfig;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.RFIDCodeDetail.mapper.CoderfidMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.RFIDCodeDetail.mapper.rfidCodeDetailMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.RFIDCodeDetail.service.RfidCodeDetailService;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.location.entity.Location;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.location.entity.LocationEntity;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.location.entity.OrderNoticeEntity;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.location.mapper.LocationMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.location.service.LocationEntityService;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.location.service.OrderNoticeService;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.pandian.entity.Checkresult;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.pandian.entity.Checkstockarea;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.pandian.mapper.CheckdiffMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.pandian.mapper.CheckresultMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.pandian.mapper.CheckstockMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.pandian.mapper.CheckstockareaMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.pandian.model.CheckProductDto;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.pandian.model.OtherInAndOut.CheckResultOtherInAndOutDto;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.pandian.model.OtherInAndOut.othOutRecords;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.pandian.model.OtherInAndOut.otherOutDataDto;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.pandian.model.checkStockListDto;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.pandian.model.checkstockMainOrder;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.stock.mapper.StockMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.stock.model.productInfo;
import com.yonyou.ucf.mdf.app.isvdxq.other.model.AccessTokenResponse;
import com.yonyou.ucf.mdf.app.isvdxq.other.service.BaseService;
import com.yonyou.ucf.mdf.app.isvdxq.secondpart.entity.EpcEntity;
import com.yonyou.ucf.mdf.app.isvdxq.secondpart.mapper.impl.TaglistMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.secondpart.model.CheckResultBean;
import com.yonyou.ucf.mdf.app.isvdxq.secondpart.service.EpclistService;
import com.yonyou.ucf.mdf.app.isvdxq.secondpart.service.StorerecordService;
import com.yonyou.ucf.mdf.app.isvdxq.utils.MyTool;
import com.yonyou.ucf.mdf.app.isvdxq.utils.RequestTool;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class CheckStockService {
    @Autowired
    CheckstockMapperImpl checkstockMapper;
    @Autowired
    StockMapperImpl stockMapper;

    @Autowired
    RfidCodeDetailService rfidCodeDetailService;
    @Autowired
    TaglistMapperImpl taglistMapper;
    @Autowired
    rfidCodeDetailMapperImpl rfidCodeDetailMapper;
    @Autowired
    CheckdiffMapperImpl checkdiffMapper;
    @Autowired
    LocationMapperImpl locationMapper;
    @Autowired
    CoderfidMapperImpl coderfidMapper;

    @Autowired
    CheckstockareaMapperImpl checkstockareaMapper;
    @Autowired
    LocationEntityService locationEntityService;
    @Autowired
    EpclistService epclistService;

    @Autowired
    CheckresultMapperImpl checkresultMapper;

    @Autowired
    BaseService baseService;

    @Autowired
    CheckresultService checkresultService;

    /**
     * 获取盘点单列表的方法
     *
     * @param checkCode   盘点单号
     * @param warehouseId 盘点仓库Id
     * @param locationID  货位Id
     * @param checkType   盘点类型
     * @param page        页面
     * @param pageSize    页面尺寸
     * @return 返回盘点单列表
     */
    public DataResult GetCheckList(String checkCode, String warehouseId, String locationID, List<String> warehouseIds, Integer checkType, int page, int pageSize, String tenantid) {

        if (warehouseId.equals(locationID)) locationID = "";      // 检测仓库ID和货位ID是否相等
        DataResult dataResult = new DataResult(1, "查询成功");
        List<checkStockListDto> resultList = new ArrayList<>();
        int total = 0;
        try {
            // 查询locationID对应的所有下级和本级内容
            List<String> locationIds = new ArrayList<>();
            if (locationID.length() > 0) {
                locationID = "%" + locationID + "%";
                List<LocationEntity> locationList = locationMapper.GetLocationListByPath(locationID, tenantid);
                if (locationList.size() > 0) {
                    for (LocationEntity item : locationList
                    ) {
                        locationIds.add(item.getId());
                    }
                }
            }
            String checkTypeString = "";
            if (checkType == null) checkTypeString = "";
            else checkTypeString = checkType.toString();

            // 参数拼接
            HashMap<String, Object> map = new HashMap<>();
            map.put("checkCode", checkCode);
            map.put("warehouseId", warehouseId);
            map.put("locationIds", locationIds);
            map.put("warehouseIds", warehouseIds);
            map.put("checkType", checkTypeString);
            map.put("page", page);
            map.put("pageSize", pageSize);

            resultList = checkstockMapper.GetCheckListLocation(map);
            total = checkstockMapper.GetCheckListTotal(map);
            if (resultList.size() <= 0) return dataResult;
            for (checkStockListDto item : resultList
            ) {
                List<Location> locationListForPathName = locationMapper.getLocationByWarehouseId(item.getWarehouseId(), tenantid);
                switch (item.getCheckType()) {
                    case "0":
                        item.setCheckType("自由盘点");
                        break;
                    case "1":
                        item.setCheckType("任务盘点");
                        break;
                    default:
                        break;
                }
                switch (item.getIsFupan()) {
                    case "1":
                        item.setIsFupan("主单");
                        break;
                    case "2":
                        item.setIsFupan("复盘单");
                        break;
                    default:
                        break;
                }
                switch (item.getSourceType()) {
                    case "1":
                        item.setSourceType("按照区域盘点");
                        break;
                    case "2":
                        item.setSourceType("按照货架盘点");
                        break;
                    case "3":
                        item.setSourceType("按照货位盘点");
                        break;
                    case "4":
                        item.setSourceType("全仓盘点");
                        break;
                    default:
                        break;
                }
                switch (item.getiStatus()) {
                    case "0":
                        item.setiStatus("未盘点");
                        break;
                    case "1":
                        item.setiStatus("盘点中");
                        break;
                    case "2":
                        item.setiStatus("完成");
                        break;
                    case "3":
                        item.setiStatus("中途作废");
                        break;
                    case "4":
                        item.setiStatus("作废");
                        break;
                    case "-1":
                        item.setiStatus("删除");
                        break;
                    default:
                        break;
                }
                item.setPathName(rfidCodeDetailService.getLocationNamesel(item.getLocationId(), locationListForPathName, ""));
            }
            dataResult.setDataList(resultList);
            dataResult.setTotalSize(total);
            return dataResult;

        } catch (Exception e) {
            dataResult.setStatus(0);
            dataResult.setMessage("查询失败");
            e.printStackTrace();
        }
        return dataResult;
    }

    /**
     * 根据货位id获取货品
     *
     * @param locationID 货位Id
     * @return 返回货品列表
     */
    public DataResult GetGoodsBylocalid(String locationID, String tenantid) {
        DataResult dataResult = null;
        List<productInfo> rfidCodeList = new ArrayList<>();
        dataResult = new DataResult(0, "查询信息失败!");

        try {
            List<String> localids = locationEntityService.GetLocationIdsByPath(locationID, tenantid);
            if (localids != null && localids.size() > 0) {
                rfidCodeList = stockMapper.selectCoderfidBylocaid(localids, tenantid);
                if (rfidCodeList != null && rfidCodeList.size() > 0) {
                    for (productInfo product : rfidCodeList) {
                        List<Location> allLocationList = locationMapper.getLocationByWarehouseId(product.getWarehouse_id(), tenantid);
                        String locationName = rfidCodeDetailService.getLocationNamesel(product.getLocation_id(), allLocationList, "");
                        product.setPathName(locationName);
                    }
                    dataResult.setData(rfidCodeList);
                    dataResult.setStatus(1);
                    dataResult.setMessage("查询信息成功！");
                    return dataResult;
                } else {
                    // dataResult.setData(null);
                    dataResult.setStatus(1);
                    dataResult.setMessage("查询信息成功！");
                    return dataResult;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataResult;
    }

    /**
     * 获取盘点单所包含的物资信息
     * 1、整仓盘点；2、部分区域或货架盘点；
     * 改写盘点范围逻辑
     *
     * @param checkId    盘点单Id
     * @param locationId 货位Id
     * @return 返回包含物资信息的DataResult
     */
    public DataResult GetCheckGoods(String checkId, String locationId, String tenantid) throws Exception {
        DataResult result = new DataResult(1, "查询成功");
        try {
            checkstockMainOrder checkstock = checkstockMapper.GetCheckStockById(checkId, tenantid);
            if (checkstock == null) {
                result.setMessage("未查找到对应的盘点单，请检查盘点单号");
                return result;
            }
            List<Checkstockarea> checkstockareaList = checkstockareaMapper.GetCheckStockAreaByCheckId(checkId, tenantid);
            if (checkstockareaList.size() <= 0) {
                result.setMessage("盘点单无任何子单据");
                return result;
            }
            List<EpcEntity> epcEntities = new ArrayList<>();
            List<LocationEntity> locationEntities = new ArrayList<>(); //当前盘点单范围内
            List<String> Ids = new ArrayList<>(); //盘点单范围内
//            for (Checkstockarea item : checkstockareaList) {
//                //todo
//                List<String> locationIds = locationEntityService.GetLocationListByPath(item.getLocationID(), tenantid)
//                        .stream().map(it -> it.getId()).collect(Collectors.toList());
//                Ids.addAll(locationIds);
//            }
            List<Location> allLocationList = locationMapper.getLocationByWarehouseId(checkstock.getWarehouseId(), tenantid);
            for (Checkstockarea item : checkstockareaList) {
                //过虑区域、货架下的货位信息
                List<Location> locationOptionalList = allLocationList.stream()
                        .filter( x ->  null!= x.getPath() && x.getPath().contains(item.getLocationID()+"|") )
                        .collect(Collectors.toList());
                if(null != locationOptionalList && locationOptionalList.size()>0){
                    for(Location location : locationOptionalList){
                        Ids.add(location.getId());
                    }
                }
            }

            if (!StringUtils.isEmpty(locationId)) {
                LocationEntity locationEntity = locationEntityService.getLocationById(locationId, tenantid);
                if (locationEntity == null) {
                    result = new DataResult(0, "货位参数异常");
                    return result;
                }
                // 整仓盘点
                if (locationEntity.getLevel() == 0) {
                    List<String> strList = new ArrayList<>();
                    strList.add(locationId);
                    epcEntities = epclistService.SelectByWIds(strList, tenantid);
                    Ids = epcEntities.stream().map(it -> it.getLocation_id()).collect(Collectors.toList());
                } else {
                    List<String> finalMyIds = Ids;
                    List<String> locationIds = locationEntityService.GetLocationIdsByPath(locationId, tenantid);
                    Ids = locationIds.stream().filter(it -> finalMyIds.contains(it)).collect(Collectors.toList());
                }
            }
            // 盘点验证盘点区域
            if (Ids.size() == 0) {
                result = new DataResult(0, "盘点范围异常");
                return result;
            }
            epcEntities = epclistService.SelectByLIds(Ids, tenantid);
            if (epcEntities.size() == 0) {
                result.setDataList(new ArrayList());
                return result;
            }
            Ids = epcEntities.stream().map(it -> it.getLocation_id()).collect(Collectors.toList());
            List<CheckProductDto> checkProductDtos = stockMapper.GetGoodsByIds(Ids, tenantid);
            HashMap<String, Object> map = new HashMap<>();
            map.put("checkId", checkId);
            map.put("Ids", Ids);
            List<CheckProductDto> checkList = checkstockMapper.GetCheckStockLog(map);
            if (checkList.size() > 0) {
                for (CheckProductDto item : checkList
                ) {
                    int checkIndex = ReturnCheckProductIndex(checkProductDtos, item);
                    if (checkIndex == -1) checkProductDtos.add(item);
                    else {
                        checkProductDtos.get(checkIndex).setLocation_id_scan(item.getLocation_id_scan());
                        checkProductDtos.get(checkIndex).setStatus(item.getStatus());
                        checkProductDtos.get(checkIndex).setiHandelMethod(item.getiHandelMethod());
                        checkProductDtos.get(checkIndex).setTagStatus(item.getTagStatus());
                    }
                }
            }
            if (checkProductDtos.size() > 0) {
                List<Location> locationList = locationMapper.getLocationByWarehouseId(checkstock.getWarehouseId(), tenantid);
                for (CheckProductDto item : checkProductDtos
                ) {
                    item.setCheckId(checkId);
                    item.setPathName(rfidCodeDetailService.getLocationNamesel(item.getLocation_id(), locationList, ""));
                    if (item.getLocation_id_scan() != null) {
                        item.setCheckPathName(rfidCodeDetailService.getLocationNamesel(item.getLocation_id_scan(), locationList, ""));
                    }
                }
                result.setDataList(checkProductDtos);
            }
        } catch (Exception ex) {
            result.setStatus(0);
            result.setMessage(ex.getMessage());
        }
        return result;
    }

    /**
     * 返回 盘点列表的物资列表中 盘点结果同时包含该物资的 物资列表下标
     *
     * @param checkProductDtos 物资列表
     * @param checkListitem    盘点结果返回的物资列表
     * @return 下标
     */
    private int ReturnCheckProductIndex(@NotNull List<CheckProductDto> checkProductDtos, @NotNull CheckProductDto checkListitem) {
        for (int i = 0; checkProductDtos != null && i < checkProductDtos.size(); i++) {
            CheckProductDto checkProductDto = checkProductDtos.get(i);
            if (checkProductDto != null) {
                if (checkProductDto.getId() != null && checkProductDto.getId().equals(checkListitem.getId())) {
                    return i;
                }
            }
        }
        return -1;
    }

    /**
     * 结束盘点单
     * 盘点区域状态不代表主单状态
     * 单据状态(0未盘点，1盘点中2完成3中途作废4作废5已创建复盘单-1删除)
     */
    @Transactional(rollbackFor = Exception.class)
    public DataResult Jieshucheck(String checkid, String userid, @NotNull String iStatus, String tenantid) {
        DataResult dataResult = new DataResult(0, "参数异常!");
        try {
            //if (!iStatus.equals("2")) return dataResult;
            checkstockMainOrder checkstockMainOrder = checkstockMapper.GetCheckStockById(checkid, tenantid);
            if (checkstockMainOrder != null && checkstockMainOrder.getiStatus() != null && checkstockMainOrder.getiStatus().equals("2")) {
                dataResult = new DataResult(1, "盘点已完成");
            }
            // 应盘点库存物料
            DataResult DataResultdto = GetCheckGoods(checkid, "", tenantid);
            if (DataResultdto == null || DataResultdto.getStatus() != 1) {
                return dataResult;
            }
            // 记录未盘点数据
            List<CheckResultBean> myDataList = new ArrayList<>();
            List<Checkstockarea> checkAreaList = new ArrayList<>();

            //取出当前盘点单所有的数据
            List<CheckProductDto> checkProductDtos = DataResultdto.getDataList();
            if (null != checkProductDtos && checkProductDtos.size() > 0) {
                List<Checkstockarea> checkstockareaList = checkstockareaMapper.GetCheckStockAreaByCheckId(checkid, tenantid);
                List<Checkresult> checkResultList = checkresultMapper.selectBycheckID(checkid, tenantid);
                List<String> areaIds = checkResultList.stream().map(it -> it.getCheckAreaID()).distinct().collect(Collectors.toList());
                checkAreaList = checkstockareaList.stream().filter(it -> areaIds.contains(it.getId())).collect(Collectors.toList());

                List<String> CoderfIDcunzai = checkResultList.stream().map(e -> e.getCoderfID()).collect(Collectors.toList());
                List<CheckProductDto> checkProductDtobidui = checkProductDtos.stream().filter(u -> !CoderfIDcunzai.contains(u.getId())).collect(Collectors.toList());
                for (CheckProductDto item : checkProductDtobidui) {
                    String checkAreaID = checkresultService.GetCheckAreaID(checkstockareaList, checkProductDtos.get(0).getLocation_id(), tenantid);
                    CheckResultBean entity = new CheckResultBean();
                    entity.setId(MyTool.getRandomId());
                    entity.setCheckID(checkid);
                    entity.setCheckCode(checkstockMainOrder.getcCheckCode());
                    entity.setCheckAreaID(checkAreaID);
                    entity.setCoderfID(item.getId());
                    entity.setProduct_id(item.getProduct_id());
                    entity.setProduct_sku_id(item.getProduct_sku_id());
                    entity.setLabelCount(String.valueOf(item.getF_stock_count()));
                    entity.setCheckLocationID(item.getLocation_id_scan());
                    entity.setStorkLocationID(item.getLocation_id());
                    entity.setScanWay("0");
                    entity.setTagStatus(String.valueOf(item.getTagStatus()));
                    entity.setTenant_id(tenantid);
                    entity.setModifier(tenantid);
                    entity.setModify_time(MyTool.GenerateTime());
                    entity.setCreator(tenantid);
                    entity.setCreate_time(MyTool.GenerateTime());
                    entity.setCheckUser(tenantid);
                    entity.setRemark("未盘");
                    entity.setCheckRemark("未盘");
                    entity.setCheckNum("0"); //盘点次数
                    entity.setiHandelMethod("0");
                    entity.setCheckstatus(String.valueOf(item.getStatus()));
                    entity.setDr(0);
                    entity.setPk_temp("");
                    entity.setVersion(0);
                    myDataList.add(entity);
                }
            }

            if (myDataList.size() > 0) {
                checkresultMapper.insertBatch(myDataList);
            }
            // 更新盘点区域状态
            if (checkAreaList.size() > 0) {
                List<String> ids = checkAreaList.stream().map(it -> it.getId()).distinct().collect(Collectors.toList());
                checkstockareaMapper.UpdateareaStatusByIds(ids, userid, iStatus, tenantid);
            }
            // 更新盘点单状态
            if (checkstockMainOrder != null) {
                checkstockMapper.UpdatCheckStatus(checkid, userid, iStatus, tenantid);
            }

            dataResult = new DataResult(1, "当前盘点单已结束!");
            return dataResult;
        } catch (Exception e) {
            dataResult = new DataResult(0, "盘点单结束失败!");
        }
        return dataResult;
    }

    /**
     * 其他出入库单据 每次只能提交一种单据
     *
     * @param idString 盘盈Json
     * @return 生成结果
     */
    public DataResult CheckResultOtherInOrOutOrder(String idString, int type, String tenantid) {
        // 查询单据提交数据
        DataResult dataResult = new DataResult(0, "无法查找对应单据，请检查数据");

        if (idString.length() > 0) {
            List<String> ids = MyTool.StringRemoveLastSymbolToList(idString, ",");
            List<CheckResultOtherInAndOutDto> otherInAndOut = checkstockMapper.OtherInOrOutOrderByCheckStockIncome(ids, type, tenantid);
            if (otherInAndOut.size() > 0) {
                // 循环提交数据
                try {
                    AccessTokenResponse res = baseService.getAccessToken3(tenantid);
                    // 获取Token
                    String accessToken = res.getAccessToken();

                    // 创建其他出库单
                    if (type == 0) {
                        //AutoCreateOtherOutOrder
                        dataResult = AutoCreateOtherOutOrder(otherInAndOut, accessToken, tenantid);
                    }
                    // 创建其他入库单
                    if (type == 2) {
                        //AutoCreateOtherInOrder
                        dataResult = new DataResult(0, "功能已升级新地址");
                        //dataResult = AutoCreateOtherInOrder(otherInAndOut, accessToken, tenantid);
                    }

                } catch (Exception exception) {
                    exception.printStackTrace();
                }
                return dataResult;
            } else {
                return dataResult;
            }
            // 得到信息后 进行后段函数提交
        } else {
            return dataResult;
        }
    }

    @Autowired
    StorerecordService storerecordService;
    @Autowired
    OrderNoticeService orderNoticeService;

    /**
     * 自动创建其他出库单
     * 盘亏出库
     *
     * @param checkResultOtherInAndOutDtos 数据结构
     * @return 创建结果
     */
    public DataResult AutoCreateOtherOutOrder(List<CheckResultOtherInAndOutDto> checkResultOtherInAndOutDtos, String token, String tenantid) throws IOException, Exception {
        DataResult dataResult = new DataResult(0, "单据回传失败");
        String ordertype = "5"; //盘亏补出
        String batch_no = MyTool.GenerateBatchnoDXQ(ordertype);
        int orderCount = 0;
        String gatewayUrl = baseService.buildBusinessUrl(tenantid);
        String url = gatewayUrl + ApiYon.getOthoutSubmit + "?access_token=" + token;
        Date date1 = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (checkResultOtherInAndOutDtos.size() > 0) {
            Map<String, Object> params = new HashMap<>();
            otherOutDataDto data = new otherOutDataDto();
            Calendar cal = Calendar.getInstance();
            Date date = cal.getTime();
            String submitcheckKey = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(date);
            data.setResubmitCheckKey(submitcheckKey);
            data.setOrg(checkResultOtherInAndOutDtos.get(0).getOrgId());
            data.setAccountOrg(checkResultOtherInAndOutDtos.get(0).getAccountOrg());
            data.setVouchdate(sdf.format(date1)); // 设置时间
            data.setBustype(DxqConfig.transtype_otherout);
            data.setWarehouse(checkResultOtherInAndOutDtos.get(0).getWarehouseCode());
            data.set_status("Insert");
            List<othOutRecords> records = new ArrayList<>();
            for (CheckResultOtherInAndOutDto item : checkResultOtherInAndOutDtos
            ) {
                othOutRecords recorder = new othOutRecords();
                recorder.setProduct(item.getStockId());
                recorder.setProductsku(item.getStockSkuId());
                recorder.setQty(item.getLabelCount());
                recorder.setSubQty(item.getLabelCount());
                recorder.setStockUnitId(item.getStockUnit());
                // 有效期 、批次
                recorder.setBatchno(item.getBatchno());
                recorder.setProductdate(item.getProductDate());
                recorder.setInvaliddate(item.getInvalidDate());
                recorder.setMemo(MyTool.makeMemoForDetail("dxq_checkdiff", item.getCheckDiffId(), item.getEpccode()));
                recorder.set_status("Insert");
                records.add(recorder);
                orderCount++;
            }
            data.setOthOutRecords(records);
            data.setMemo(MyTool.makeMemo(batch_no, orderCount));
            params.put("data", data);
            String resString = RequestTool.doPost(url, JSON.toJSONString(params));
            JSONObject jsonObject = JSONObject.parseObject(resString);
            if (jsonObject.getString("code").equals("200")) {
                String dataDetail = jsonObject.getString("data");
                dataResult = storerecordService.UpdateForOutCallback(checkResultOtherInAndOutDtos, tenantid, ordertype, dataDetail, batch_no);
                if (dataResult.getStatus() != 1) {
                    //无单据回写
//                    orderNoticeService.EditEntityStatusForPick(orderid, DxqConfig.AllotOk, 1, DxqConfig.StatusFail, userId, pickbatchno);
                    try {
                        OrderNoticeEntity recordEntity = new OrderNoticeEntity();
                        recordEntity.setTenant_id(tenantid);
                        recordEntity.setActioncode("pc_callback");
                        recordEntity.setPickbatchno(batch_no);
                        recordEntity.setType(DxqConfig.TypeFail); //本地回写失败
                        recordEntity.setRemarkyonyou(dataDetail);
                        recordEntity.setRemarkdxq(JSONObject.toJSONString(checkResultOtherInAndOutDtos));
                        recordEntity.setRemark(ordertype);
                        recordEntity.setStatus(0);
                        orderNoticeService.InsertEntity(recordEntity);
                    } catch (Exception ex) {
                        System.out.println("[其他入库]" + ex.getMessage());
                    }
                    return dataResult;
                }

            } else {
                String Message = jsonObject.getString("message");
                dataResult.setMessage(Message);
                return dataResult;
            }
            dataResult.setMessage("成功将 【 " + orderCount + " 】条数据完成其他出库创建");
            return dataResult;
        } else {

            dataResult.setMessage("没有相应单据信息请检查提交的数据");
            return dataResult;
        }
    }


    public List<CheckResultOtherInAndOutDto> OtherInOrOutOrderByCheckStockIncome(List<String> ids, Integer type, String tenantid) {
        List<CheckResultOtherInAndOutDto> otherInAndOut = checkstockMapper.OtherInOrOutOrderByCheckStockIncome(ids, type, tenantid);
        return otherInAndOut;
    }
}
